<template>
	<view>
		<view class="formModel">
			<view class="cu-form-group" @tap="Select()">
				<!-- @change="PickerChange" :value="index" :range="pickerArr" -->
				<view class="title">搜索选择已录入的养殖场</view>
				<picker disabled="true"   >
					<view class="picker" v-if="data.farmName">
						{{data.farmName}}
					</view>
					<view class="picker" v-else>
						无
					</view>
				</picker>
			</view>
			<template v-if="index==0">
			<view class="cu-form-group">
				<view class="title">没有找到自家的养殖场？</view>
				<picker disabled="true">
					<view style="color: #00aaff;" class="picker" @tap="toFarmFilePost">
						前往新增
					</view>
				</picker>
			</view>
			</template>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				index: 0,
				pickerArr:['无'],
				farmName:'',
				farmId:'',
				data: []
			};
		},
		onLoad:function(data){
			console.log(data)
			if(data){
				this.data=data;
			}
		},
		onReady() {
			//获取已经录入的养殖场信息表
			// this.getFarmList()
		},
		// onBackPress(e){
		// 	console.log(e)
		// 	uni.switchTab({
		// 		url:"/pages/productionAdmin/productionAdmin"
		// 	})
		// },
		onNavigationBarButtonTap(e){
			console.log(this.User)
			if(this.User.userRight.name==="livestock"){
				uni.showToast({
					title:"您已拥有养殖场账号权限！",
					icon:"none"
				});return;
			}
			this.postFarmInfo()
		},
		methods:{
			//前往搜索选择养殖场
			Select(){
				uni.navigateTo({
					url:"farmerSelect"
				})
			},
			PickerChange(e) {
				this.index = e.detail.value;
				console.log(this.picker[this.index])
			},
			//前往新增
			toFarmFilePost(){
				if(this.User.userRight.name==="livestock"){
					uni.showToast({
						title:"您已拥有养殖场账号权限！",
						icon:"none"
					});return;
				}
				uni.navigateTo({
					url:"./farmFilePost"
				})
			},
			//提交养殖场信息
			postFarmInfo(){
				if(!this.data.farmId){
					uni.showToast({
						icon:"none",
						title:"请选择养殖场"
					});
					return;
				}
				console.log(11)
				uni.request({
					url:this.confige.webUrl+"/aquaculture/auditInformation",
					data:{
						farm_id:this.data.farmId
					},
					header:{
						'X-Token':this.User.token
					},
					success: (res) => {
						console.log(res)
						switch(res.data.code/100){
							case 2:
							uni.showModal({
								title:"提示",
								showCancel:false,
								content:"您的申请已成功提交，请等待审核",
							    success:(res)=> {
									if (res.confirm) {
									uni.redirectTo({
										url:"../productionAdmin"
									})
									} 
								}
							});
							break;
							case 4:
							uni.showModal({
								title:"提示",
								showCancel:false,
								content:res.data.message,
							    success:(res)=> {
									if (res.confirm) {
									uni.redirectTo({
										url:"../productionAdmin"
									})
									} 
								}
							});
							case 5:
							uni.showToast({
								icon:"none",
								title:res.data.message
							})
						}
					}
				})
			},
			//获取已经录入的养殖场信息表
			getFarmList(){
				uni.request({
					url:this.confige.webUrl+"/aquaculture/sddCultivation",
					data:{
					},
					header:{
						'X-Token':this.User.token
					},
					success: (res) => {
						let list = res.data.result;
						console.log(list)
						let arr = [];
						for(let i=0;i<list.length;i++){
							arr.push(list[i].farm_name)
						}
						this.pickerArr.push(...arr);
						let arr1 = [];
						for(let y=0;y<list.length;y++){
							arr1.push({
								farm_id:list[y].farm_id,
								farm_name:list[y].farm_name
							})
						}
						this.picker.push(...arr1)
					}
				})
			}
		}
	}
</script>

<style lang="less">
@import "../../../common/colorui/main.css";
@import "../../../common/colorui/icon.css";
@import "../../../common/global.less";
.formModel{
background-color:@backcolorthree;
border-bottom:2rpx solid #eaeaea;
.cu-form-group {
.title {min-width: calc(4em + 15px);}		
}
}
</style>
